<?php
/***
 * @info 微信相关操作
 * @author lcy
 * @date   二〇一七年一月七日 20:00:59
 */
namespace Weixin\Controller;
use Common\Controller\WeixinbaseController;

class OauthController extends WeixinbaseController {
    protected $wx_oauth;
    public function _initialize() {
        $this->wx_oauth = D("Common/WxOauth");
        parent::_initialize();
    }
    /***
     * @info 用户每次点击一次菜单，系统会做一次登录操作
     */
    public function index(){
        $config = $this->wx_model->where("id=1")->find(); /**获取微信配置信息**/
        $weixin = new \Weixin\Wechat($config);
        $id = intval($_GET['id']);
        $oid = intval($_GET['oid']);
        if($_GET['code']){
            $uid = isset($_SESSION['uid'])? $_SESSION['uid'] : 0;
            $personID = isset($_SESSION['personID'])? $_SESSION['personID'] : 0;
            $json = $weixin->getOauthAccessToken();
            if(!empty($json)){
                $wxid = $json['openid'];/***获取openID**/
                $_SESSION['openID'] = $wxid;  /**openID 添加至session中*/
                $userInfo = $this->wx_user->where("open_id='$wxid'")->find();  /**根据openID 查询用户**/
                if(empty($userInfo) && $uid == 0){
                    $info = $weixin->getUserInfo($wxid);  /**根据openID获取微信用户信息**/
                    if($info)
                        $this->followUser($wxid,$info); /**用户进行系统关注或更新操作***/
                        $userInfo = $this->wx_user->where("open_id='$wxid'")->find();
                }
                /***根据openID 查询员工，判断员工是否已经绑定帐号*/
                $personInfo = $this->wx_person->where("open_id='$wxid'")->find();
                if(!empty($personInfo) && $personID ==0){
                	$_SESSION['personID'] = $personInfo['id'];
                }
                $_SESSION['uid'] = $userInfo['uid'];
            }else{
                $_SESSION['uid'] = 0;
            }
            $url = U("weixin/oauth/index");
            if($oid > 0){  //统计菜单的点击次数
                $oauth_info = $this->wx_oauth->where("oid=$oid")->find();
                $url = $oauth_info['weburl'];
                $oauth_info['click'] = $oauth_info['click']+1;
                $this->wx_oauth->save($oauth_info);
            }
            header("Location:$url");
            exit;
        }
        $baseurl = $_SERVER['SERVER_NAME'] ? "http://".$_SERVER['SERVER_NAME'] : "http://".$_SERVER['HTTP_HOST'];
        $url = $baseurl.U("weixin/oauth/index",array("id" => $id, "oid" => $oid));
        //$url = $weixin->getOauthRedirect($url,1,'snsapi_useinfo');
        $url = $weixin->getOauthRedirect($url,1,'snsapi_base');
        header("Location:$url");
        exit;
    }
    
    //关注
    function followUser($wxid,$info=array()){
        $nickname = $info['nickname'];
        $sex = intval($info['sex']);
        $country = $info['country'];
        $province = $info['province'];
        $city = $info['city'];
        $access_token = $info['access_token'];
        $headimgurl = $info['headimgurl'];
        $expire_in = time()+48*3600;
        $wx_user = $this->wx_user->where("open_id='$wxid'")->field("uid")->find();
        $from_id = intval($_GET['id']) > 0 ? intval($_GET['id']) : 1 ;
        if(count($wx_user)>0 && $wx_user['uid']>0){
            $wx_user['open_id'] = $wxid;
            $wx_user['nickname'] = $nickname;
            $wx_user['sex'] = $sex;
            $wx_user['country'] = $country;
            $wx_user['province'] = $province;
            $wx_user['city'] = $city;
            $wx_user['access_token'] = $access_token;
            $wx_user['expire_in'] = $expire_in;
            $wx_user['headimgurl'] = $headimgurl;
            $wx_user['from_id'] = $from_id;
            $wx_user['isfollow'] = 1;
            $result=$this->wx_user->save($wx_user);
        }else{
            $createtime = time();
            $createymd = date('Y-m-d');
            $wx_user['open_id'] = $wxid;
            $wx_user['nickname'] = $nickname;
            $wx_user['sex'] = $sex;
            $wx_user['country'] = $country;
            $wx_user['province'] = $province;
            $wx_user['city'] = $city;
            $wx_user['access_token'] = $access_token;
            $wx_user['expire_in'] = $expire_in;
            $wx_user['headimgurl'] = $headimgurl;
            $wx_user['createtime'] = $createtime;
            $wx_user['createymd'] = $createymd;
            $wx_user['isfollow'] = 1;
            $wx_user['from_id'] = $from_id;
            $result=$this->wx_user->add($wx_user);
        }
        return true;
    }
}

